//
//  NumberSearchVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/7/25.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "NumberSearchVC.h"
#import "NumberSearchResultVC.h"
#import "NumberSearchCell.h"
@interface NumberSearchVC ()<UISearchResultsUpdating,UISearchBarDelegate,UISearchControllerDelegate,NumberSearchResultVCDelegate,UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UISearchController * searchController;

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NumberSearchResultVC *searchResultVC;

@end

@implementation NumberSearchVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.hidesBackButton = YES;
    self.barBGAlpha = 1;
    [self setup];
}


- (void)setup {
    
    self.tableView= [[UITableView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kNavigationBarHeight ) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"NumberSearchCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self setUpSearchController];
    
    // ios 11  scrollvew 的位置调整
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior =UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}


- (void)setUpSearchController {
    
    //创建 搜索使用的表示图控制器
    _searchResultVC = [[NumberSearchResultVC alloc] initWithStyle:UITableViewStylePlain];
    _searchResultVC.delegate = self;
    //tvc.view.backgroundColor = [UIColor blueColor];
    
    //创建搜索控制器
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultVC];
    _searchController.searchResultsUpdater = self;
    _searchController.delegate = self;
    _searchController.searchBar.delegate = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    _searchController.hidesNavigationBarDuringPresentation = NO;
    _searchController.searchBar.frame = CGRectMake(15, 20, SCREEN_WIDTH - 30, 44.0);
    _searchController.searchBar.showsCancelButton = YES;
    
    _searchController.searchBar.placeholder = MYLocalizedString(@"SITEPLAY_SEARCH_PLACEHOLD", nil);
    
    _searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
    
    _searchController.searchBar.barTintColor = [UIColor whiteColor];
    _searchController.searchBar.tintColor = [UIColor whiteColor];
    
    [_searchController.searchBar sizeToFit];
    
    
    CGFloat height;
    if (@available(iOS 11.0, *)) {
        
        height = 32.0f;
        [_searchController.searchBar setSearchFieldBackgroundPositionAdjustment: UIOffsetMake(0, -3)];
    }else{
        height = 32.0f;
    }
    UIImage* searchBarBg = [IGUtils getImageWithColor:[UIColor colorWithWhite:1 alpha:0.3] andHeight:height];
    
    [_searchController.searchBar setBackgroundImage:searchBarBg];
    [_searchController.searchBar setBackgroundColor:[UIColor clearColor]];
    [_searchController.searchBar setSearchFieldBackgroundImage:searchBarBg forState:UIControlStateNormal];
    
    [_searchController.searchBar setImage:[UIImage imageNamed:@"clear_placeholder"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
    [_searchController.searchBar setImage:[UIImage imageNamed:@"clear_placeholder"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
    UITextField *searchField = [_searchController.searchBar valueForKey:@"searchField"];
    searchField.tintColor = [UIColor whiteColor];
    searchField.textColor = [UIColor whiteColor];
    searchField.font = Font(14);
    [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    
    if (searchField) {
        searchField.layer.cornerRadius = 3.0f;
        searchField.layer.masksToBounds = YES;
    }
    
    UIImage *image = [UIImage imageNamed:@"searchIcon"];
    UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
    iconView.frame = CGRectMake(0, 0, 16 , 16);
    searchField.leftView = iconView;
    
    self.navigationItem.titleView = _searchController.searchBar;
    
}


#pragma mark - search

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    
    NSString *searchString = [self.searchController.searchBar text];
    
    if (searchString.length > 0) {
        //过滤数据
        
        self.searchResultVC.searchResultArr = [self getScenicPointWithMatcch:searchString];
        //刷新表格
        [self.searchResultVC.tableView reloadData];
        
    }
}


- (NSMutableArray *)getScenicPointWithMatcch:(NSString *)str {
    
    NSMutableArray * resultArray = [NSMutableArray new];
    
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF.keywords contains[cd] %@", str];
    
    //过滤数据
    resultArray = [NSMutableArray arrayWithArray:[self.ScenicPointsArray filteredArrayUsingPredicate:preicate]];
    
    return  resultArray;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    
    searchBar.placeholder = @"";
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    
    searchBar.placeholder = MYLocalizedString(@"SITEPLAY_SEARCH_PLACEHOLD", nil);
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma makr - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.ScenicPointsArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NumberSearchCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    ScenicPoint * model = self.ScenicPointsArray[indexPath.row];
    
    [cell configDataWith:model];
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 75;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicPoint * point = self.ScenicPointsArray[indexPath.row];
    
    [self selectSenicPoint:point];
}


//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//
//    if (section == 1) {
//        UITableViewHeaderFooterView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HeaderView"];
//        headerView.textLabel.text = MYLocalizedString(@"CHANGECITY_NEARBY", nil);
//        headerView.textLabel.textColor = kContentColor;
//        headerView.textLabel.font = Font(12);
//
//        return headerView;
//    }
//
//    return nil;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return  0.000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.00001;
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [_searchController.searchBar endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -NumberSearchResultVCDelegate

- (void)selectSenicPoint:(ScenicPoint *)point {
    
    [self dismissViewControllerAnimated:YES completion:nil];
    [self dismissViewControllerAnimated:YES completion:nil];
    if ([_delegate respondsToSelector:@selector(selectSenicPoint:)]) {
        [_delegate selectSenicPoint:point];
    }
}



/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
